package com.yzqp.format;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yzqp.common.util.FastJsonUtil;

import java.io.*;
import java.util.List;

public class BaseFormatUtil {

    public static String basePath = "D:\\developSoft\\project\\";

    /**
     * 微信json 转化为常规json
     * @param data
     * @return
     */
    public static String wxJsonToCommonJson(String data){
        data.replaceAll("}", "},");
        data = "[" + data + "]";
        System.out.println(data);
        return data;
    }

    /**
     * 格式化 导入json
     * @param path
     * @param jsonArray
     */
    public static void formatJson(String path, JSONArray jsonArray){

            // 写入数据
            BufferedWriter writer = null;
            try {
                // 声明一个文件
                File f = new File(path);
                // 获取父文件夹
                File parentDir = f.getParentFile();
                // 不存在文件夹，就创建文件夹
                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }

                FileOutputStream writerStream = new FileOutputStream(path);
                writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
                for (int i = 0; i < jsonArray.size() ; i ++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    writer.write(jsonObject.toJSONString() + "\n");
                }


            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


    }
}
